package 图片平滑器661;

public class Solution {
    public int[][] imageSmoother(int[][] img) {
        int[][] out = new int[img.length][img[0].length];
        for (int i = 0; i < img.length; i++) {
            for (int j = 0; j < img[i].length; j++) {
                out[i][j] = get(i, j, img);
            }
        }
        return out;
    }
    int get(int i, int j, int[][] arr) {
        int count = 1;
        int sum = arr[i][j];
        if(i > 0) {
            count++;
            sum += arr[i - 1][j];
        }
        if(j > 0) {
            count++;
            sum += arr[i][j - 1];
        }
        if(i < arr.length - 1) {
            count++;
            sum += arr[i + 1][j];
        }
        if(j < arr[0].length - 1) {
            count++;
            sum += arr[i][j + 1];
        }
        if(i > 0 && j > 0) {
            count++;
            sum += arr[i - 1][j - 1];
        }
        if(i > 0 && j < arr[0].length - 1) {
            count++;
            sum += arr[i - 1][j + 1];
        }
        if(i < arr.length - 1 && j > 0) {
            count++;
            sum += arr[i + 1][j - 1];
        }
        if(i < arr.length - 1 && j < arr[0].length - 1) {
            count++;
            sum += arr[i + 1][j + 1];
        }
        //System.out.println(sum + " " + count);
        return sum / count;
    }
}
